/*
 *  有道在线翻译
 *
 * VERSION  DATE        BY              REASON
 * -------- ----------- --------------- ------------------------------------------
 * 1.00     2015-10-29  wxg      程序.发布
 * -------- ----------- --------------- ------------------------------------------
 * Copyright 2015 easycode2  System. - All Rights Reserved.
 *
 */
package cn.com.softvan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class YoudaoTranslate {

	private static final String	url		= "http://fanyi.youdao.com/openapi.do";

	private static final String	keyfrom	= "wuxiaogangtest1";
	private static final String	key		= "1069496433";

	private static final String	doctype	= "json";

	private static String sendGet(String str) {
		// 编码成UTF-8
		try {
			str = URLEncoder.encode( str, "utf-8" );
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		StringBuffer buf = new StringBuffer();
		buf.append( "keyfrom=" );
		buf.append( keyfrom );
		buf.append( "&key=" );
		buf.append( key );
		buf.append( "&type=data&doctype=" );
		buf.append( doctype );
		buf.append( "&version=1.1&q=" );
		buf.append( str );
		String param = buf.toString();
		String result = "";
		BufferedReader in = null;
		try {
			String urlName = url + "?" + param;
			URL realUrl = new URL( urlName );
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty( "accept", "*" );

			in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );

			String lines = "";
			while ((lines = in.readLine()) != null) {
				result += lines;
			}
			// System.out.println("====="+result);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String getYouDaoValue(String str) {
		if(!isContainsChinese( str )){
			//字符串不包含中文 直接返回
			return str;
		}
		String result = null;
		// 发送GET请求翻译
//		result = sendGet( str );
		// // 处理JSON的值
		// result="{\"translation\":[\"TongLi club\"],\"query\":\"测试一段文字\",\"errorCode\":0}";
		// // 处理JSON的值
		JSONObject jo = JSONObject.fromObject( result );
		String in = jo.getString( "errorCode" );
		if (in.equals( "0" )) {
			System.out.println( result );
			JSONArray ja;
			try {
				ja = new JSONArray().fromObject( jo.getString( "translation" ));
			} catch (Exception e) {
				e.printStackTrace();
				return str;
			}
			// System.out.println("有道翻译====>=="+jo.getString("query")+"==="+ja.getString(0)
			// );
			return ja.getString( 0 ) + " ";
		} else {
			if (in.equals( "20" )) {
				System.out.println( "要翻译的文本过长" );
				// return "要翻译的文本过长";
			} else if (in.equals( "30" )) {
				System.out.println( "无法进行有效的翻译" );
				// return "无法进行有效的翻译";
			} else if (in.equals( "40" )) {
				System.out.println( "不支持的语言类型" );
				// return "不支持的语言类型";
			} else if (in.equals( "50" )) {
				System.out.println( "无效的key" );
				// return "无效的key";
			}
			return str;
		}
	}
	/**判断是否包含中文*/
	public static boolean isContainsChinese(String str) {
		Matcher matcher = Pattern.compile("[\u4e00-\u9fa5]").matcher( str );
		boolean flg = false;
		if (matcher.find()) {
			flg = true;
		}
		return flg;
	}

	public static void main(String[] args) {
//		 String str = "test一段测试文字";
//		 String temp = YoudaoTranslate.getYouDaoValue( str );
//		 System.out.println( temp );
//		String s1 = "Temporarily no data哈!";
//		if (isContainsChinese(s1)) {
//			System.out.println( "包含中文" );
//		}else{
//			System.out.println( "没有中文" );
//		}
		System.out.println("预定发送时间\t\t\t            ".replaceAll("\r|\n|\t| ", ""));
	}
}
